const express=require('express')
const router= express.Router();
const db=require('../../utils/db')


//渲染表格
router.get('/xuanran',function(req,res){
  let mysql=`SELECT * FROM caipindangan `
  db(mysql,null,function(err,data){
    if(err){
        return res.json({
            code: 500,
            msg: '查询菜品失败，请稍后重试'
        })
    }
    res.json({
        code: 200,
        msg: '查询菜品成功',
        data: data
    })
})
})
//删除
router.post('/shuanchu',function(req,res){
    let str
    if(req.body.numb==1){
       str=req.body.data
    }else{
        str=req.body.data.toString()
    }
    let mysql=`  DELETE FROM caipindangan WHERE id in (${str})`
    db(mysql,null,function(err,data){
      if(err){
          return res.json({
              code: 500,
              msg: '删除失败，请稍后重试'
          })
      }
      res.json({
          code: 200,
          msg: '删除成功'
      })
  })
  })


  //编辑
router.post('/bianji',function(req,res){
    let mysql=`UPDATE caipindangan SET name =${req.body.name},cpid =${req.body.cpid},money =${req.body.money},leibie =${req.body.leibie},kouwei =${req.body.kouwei},guige =${req.body.guige},danwei =${req.body.danwei},zhuangtai =${req.body.zhuangtai} WHERE id =${req.body.id}`
    db(mysql,null,function(err,data){
      if(err){
          return res.json({
              code: 500,
              msg: '修改失败，请稍后重试'
          })
      }
      res.json({
          code: 200,
          msg: '修改成功'
      })
  })
  })
module.exports = router